package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.HRParamBean;
import com.attendance.www.hr.beans.RequestApprovalPageBean;
import com.attendance.www.hr.beans.RequestApprovalSaveBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IHRService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * HR驳回申请Controller
 * @author WB  ZX
 * 2017-11-19
 */
@Controller
@RequestMapping("/hr")
public class HRController {
	@Resource
	private IHRService hrService;
	
	/**
	 * 查询申请信息
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/getRequestList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRequestList(HttpServletRequest request, @RequestBody HRParamBean paramBean) {
		
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		
		Integer pageNo =paramBean.getPageNo();
		Integer pageCount = paramBean.getPageCount();
		if (pageNo ==null||pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		RequestApprovalPageBean  requestPageBean=hrService.getRequestList(paramBean,userEmployee.getEmployeeId(),startNum, pageCount);
		return ResultMap.success(requestPageBean);
	}
	
	/**
	 * HR驳回申请（没有用到）
	 * @param request
	 * @param requestBean
	 * @return
	 */
	@RequestMapping(value = "/getRejectRequest.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRejectRequest(HttpServletRequest request, @RequestBody RequestApprovalSaveBean requestApprovalSaveBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		hrService.getRejectRequest(requestApprovalSaveBean,userEmployee.getEmployeeId());
		return ResultMap.success();
	}
	
	/**
	 * HR驳回补签
	 * @param request
	 * @param requestApprovalSaveBean
	 * @return
	 */
	@RequestMapping(value = "/saveRejectRetroactive.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveRejectRetroactive(HttpServletRequest request, @RequestBody RequestApprovalSaveBean requestApprovalSaveBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if (hrService.saveRejectRetroactive(requestApprovalSaveBean,userEmployee.getEmployeeId())) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00018");
	}

}
